app.service('dataHistoryService',['$q','utilService','$http',function($q,utilService,$http){

    //查询店铺下拉框
    this.getShopList = function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url: $scope.app.domain + '/shop/list'
        }).then(function (response) {
            deferred.resolve(response.data);
        }, function (e) {
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };

    this.exportData = function($scope){
        $http({
            method:'post',
            url:$scope.app.domain + 'report/orders/history',
            data:$scope.ordersAndGoodsVO,
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var fileName =$scope.ordersAndGoodsVO.start + '至' + $scope.ordersAndGoodsVO.end + '订单及商品历史数据表.xls';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    };

    //查询订单对应的商品历史数据
    this.query = function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url: $scope.app.domain + 'goods/data',
            params:{ordersDataHistoryId:$scope.goodsHistory.ordersDataHistoryId}
        }).then(function (response) {
            deferred.resolve(response.data);
        }, function (e) {
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };
}]);